package com.github.freewu.commons.utils;


import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lqkj.commons.R;


/**
 * toast工具类
 */
public class ToastUtil {

    public static void showShort(Context context, String info) {
        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);

        View mView = View.inflate(context, R.layout.normal_toast_layout, null);
        TextView toastText = (TextView) mView.findViewById(R.id.toast_text);
        toastText.setText(info);
        toast.setView(mView);

        ObservableToast(toast);
    }

    public static void showShort(Context context, @StringRes int info) {
        showShort(context, context.getString(info));
    }

    public static void showShortTrue(Context context, String str) {
        showToastWithIco(context, str, R.drawable.true_taost_bg);
    }

    public static void showShortError(Context context, String str) {
        showToastWithIco(context, str, R.drawable.error_taost_bg);
    }

    public static void showShortWarn(Context context, String str) {
        showToastWithIco(context, str, R.drawable.warn_taost_bg);
    }

    private static void showToastWithIco(@NonNull Context context, @NonNull String str, @NonNull @DrawableRes int bgRes) {
        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);

        View mView = View.inflate(context, R.layout.toast_layout, null);
        TextView text = (TextView) mView.findViewById(R.id.toast_text);
        ImageView toastImg = (ImageView) mView.findViewById(R.id.toast_bg);
        text.setText(str);
        toastImg.setImageResource(bgRes);

        toast.setView(mView);

        ObservableToast(toast);
    }

    private static void ObservableToast(Toast toast) {
        toast.show();
    }
}
